# testApp3.py
# AUTHOR: LEWIS MARHIN
# PURPOSE: To test and prototype GUI for CP304x Student Day Planner project.
# CONTROLS: RIGHT SOFT KEY : Exit

# Some code based off Nokia's ball.py program (standard with S60 python
# intstallation)

sys.path.append("c:\\python")
sys.path.append("e:\\python")
sys.path.append("c:\\resource")
sys.path.append("e:\\resource")

import e32
import appuifw
import Cursor
import KeyHandler
import sysinfo
import sys
from appuifw import *
from graphics import *
from key_codes import *

class GUI:
	def __init__(self)
		self.app_lock = e32.Ao_lock()

		# Used by event drawer
		self.eventFlicker = 0
		self.eventHeight = 20

		# Used to draw numbers down the side (for demonstration purposes)
		self.hours = ["7","8","9","10","11","12","13","14","15","16","17","18","19","20","21"]

		self.img = Image.new(sysinfo.display_pixels()) # Possibly is width x height. Should change to
													 # getWidth() and getHeight() which doesn't exist
													 # in python s60, probably something similiar does
													 # though.

	def display(rect):
		canvas.blit(self.img)

	def drawEvent(boxNum, cursorOver):
		# positionOffset
		pO = boxNum * height
		
		self.img.rectangle([(0, 56 + pO), (sysinfo.display_pixels()[1], 76 + pO)], 0xCCCCCC, 0xCCCCCC)
		if cursorOver == True:
			if self.flicker == 1:
				self.flicker = 0
				self.img.rectangle([(0, 56 + pO), (sysinfo.display_pixels()[1], 76 + pO)], 0xF0F0F0, 0x0000000)
			else:
				self.flicker = 1

	# This var and function used to exit program
	run = 1
	def quit():
		reload(cursor)
		global run
		run = 0
		#app_lock.signal()
	appuifw.app.exit_key_handler = quit

	keyboard = KeyHandler.Keyboard()
	keys = KeyHandler.Keys()

	canvas = appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback = display) # Create new canvas
	appuifw.app.body = canvas # Sets the screen to show the canvas object
	appuifw.app.screen = 'full' # Fullscreen mode

	# This function works like in OpenGL, it will loop
	while run:
		self.img.clear(0xFFFFFF) # clears canvas for redraw (probably)
		#img.point((locX + size /2, locY + size /2), 0x000000, width = size)#draw dot

		# EVENT #
		DrawHandler.Draw.drawEvent(0, True, self.eventFlicker, self.eventHeight)
		drawEvent(1, False)
		drawEvent(2, False)
		# END EVENT #

		Cursor.ScrollType.moveCursor(keys.arrows(amount))
		
		Cursor.ScrollType.drawCursor(self.img)

		# TEXT #
		# 3. Will change CURRENT_DATE to the actual current date later
		self.img.text([(5, 20)], u"Student Day Planner", font = 'normal') # style
		self.img.text([(5, 45)], u"CURRENT_DATE", font = (u'LatinBold12', 15, appuifw.STYLE_BOLD)) # font
		# Line below, when uncommented, prevents either line from being rendered
		#img.text([(5, 45)], u"CURRENT_DATE", font = u'LatinBold12') # font

		# 1.    Goes through list of hours and prints them with a 0 up front if
		#       it is before 10
		# 2.    Font style is set to 'dense' which is small and squished (normal text)
		tmp = 0
		for hour in hours:
			if int(hour) <= 9:
				img.text([1, 72 + tmp], u" " + unicode(hour) + u":00", font = 'dense')
			else:
				img.text([1, 72 + tmp], unicode(hour) + u":00", font = 'dense')
			tmp+= 20
		# END TEXT #

		# LINES #
		# img.line([(x origin, y origin), (x dest., y dest.)], colour, options)
		# (See API 6.4.3 I think it is)
		# Change to black lines for demonstration
		self.img.line([(50, 50), (50, 400)], 0xFF0000, width = 3) # draw red line
		self.img.line([(0, 25), (350, 25)], 0x00FF00, width = 3) # draw blue line
		self.img.line([(0, 50), (350, 50)], 0x0000FF, width = 3) # draw green line
		# END LINES #
		
		canvas.blit(self.img)
		canvas.blit(self.img)
		canvas.blit(self.img)
		canvas.blit(self.img)
		e32.ao_yield() # Allows priority to be moved elsewhere.
		#display(())
    
app_lock.wait()
